package com.uenpay.im.ui.old.activity.dynamic;

import com.qiyunxin.android.http.HttpInvokeResultCode;
import com.qiyunxin.android.http.net.HttpRequestType;
import com.uenpay.im.IMApplication;
import com.uenpay.im.R;
import com.uenpay.im.entities.HttpInvokeResult;
import com.uenpay.im.invokeitems.HttpRequestHandle;

import java.util.ArrayList;

/**
 * @author SL
 * @created 2016-1-29 1:58:36
 * @copyRight http://www.qiyunxin.com
 * @function 动态管理
 */
public class DynamicHandle {

	public interface IGetDynamicListResultListener {
		void onGetDynamicResult(int status, String msg,
										 ArrayList<DynamicEntity> dynamics);
	}

	public DynamicHandle() {

	}

	/**
	 * 获取好友动态列表
	 *
	 * @param page
	 * @param mIGetDynamicListResultListener
	 */
	public void getDynamicList(final int page,
			final IGetDynamicListResultListener mIGetDynamicListResultListener) {

		GetDynamicsInvokeItem item = new GetDynamicsInvokeItem(page);
		HttpRequestHandle.onBaseRequest(item,
				HttpRequestType.SERVER_FAIL_AND_CACHE,
				new HttpRequestHandle.IRequestResultListener() {

					@Override
					public void onResult(Object resultObject, boolean isCache,
										 String failReason) {
						if (resultObject != null) {
							GetDynamicsInvokeItem.GetDynamicsResult result = ((GetDynamicsInvokeItem) resultObject)
									.getOutPut();
							if (page == 1 && !isCache) {
								IMApplication.config
										.setDynamicLastSendCustId("");
							}
							if (result != null) {
								if (mIGetDynamicListResultListener != null) {
									mIGetDynamicListResultListener
											.onGetDynamicResult(result.status,
													result.msg, result.dynamics);
								}
							}
						} else {
							if (mIGetDynamicListResultListener != null) {
								mIGetDynamicListResultListener
										.onGetDynamicResult(-1, failReason,
												null);
							}
						}
					}
				});

	}

	public interface IGetDynamicPersonalListResultListener {
		void onGetDynamicPersonalResult(int status, String msg,
												 ArrayList<DynamicEntity> dynamics, String dynamicBgFileId,
												 String dynamicBgFileHash);
	}

	/**
	 * 获取个人动态列表
	 *
	 * @param page
	 * @param cust_id
	 * @param mIGetDynamicPersonalListResultListener
	 */
	public void getDynamicPersonalList(
			int page,
			String cust_id,
			final IGetDynamicPersonalListResultListener mIGetDynamicPersonalListResultListener) {
		getDynamicByCustInvokeItem item = new getDynamicByCustInvokeItem(page,
				cust_id);
		HttpRequestHandle.onBaseRequest(item,
				HttpRequestType.SERVER_FAIL_AND_CACHE,
				new HttpRequestHandle.IRequestResultListener() {

					@Override
					public void onResult(Object resultObject, boolean isCache,
							String failReason) {
						if (resultObject != null) {
							getDynamicByCustInvokeItem.getDynamicByCustInvokeItemResult result = ((getDynamicByCustInvokeItem) resultObject)
									.getOutPut();
							if (result != null) {
								if (mIGetDynamicPersonalListResultListener != null) {
									mIGetDynamicPersonalListResultListener
											.onGetDynamicPersonalResult(
													result.status, result.msg,
													result.dynamics,
													result.dynamicBgFileId,
													result.dynamicBgFileHash);
								}
							} else {
								if (mIGetDynamicPersonalListResultListener != null) {
									mIGetDynamicPersonalListResultListener
											.onGetDynamicPersonalResult(-1, "",
													null, "", "");
								}
							}
						} else {
							if (mIGetDynamicPersonalListResultListener != null) {
								mIGetDynamicPersonalListResultListener
										.onGetDynamicPersonalResult(-1, "",
												null, "", "");
							}
						}
					}
				});
	}

	public interface IGetDynamicHistoryReply {
		void onGetDynamicHistoryReplyResult(int status,
															String msg, ArrayList<Reply> replys, boolean isCache);
	}

	/**
	 * 获取动态历史评论记录
	 */
	public void getDynamicHistoryReplys(int page,
			final IGetDynamicHistoryReply mIGetDynamicHistoryReply) {
		GetDynamicsHistoryInvokeItem item = new GetDynamicsHistoryInvokeItem(
				page);

		HttpRequestHandle.onBaseRequest(item,
				HttpRequestType.SERVER_FAIL_AND_CACHE,
				new HttpRequestHandle.IRequestResultListener() {

					@Override
					public void onResult(Object resultObject, boolean isCache,
							String failReason) {
						if (resultObject != null) {

							GetDynamicsHistoryInvokeItem.GetDynamicsHistoryInvokeItemResult result = ((GetDynamicsHistoryInvokeItem) resultObject)
									.getOutPut();
							if (result != null) {
								mIGetDynamicHistoryReply
										.onGetDynamicHistoryReplyResult(
												result.status, result.msg,
												result.replys, isCache);
							}
						} else {
							if (mIGetDynamicHistoryReply != null) {
								mIGetDynamicHistoryReply
										.onGetDynamicHistoryReplyResult(-1,
												failReason, null, isCache);
							}
						}
					}
				});
	}

	/**
	 * 获取动态详情
	 *
	 * @param dynamicid
	 * @param mIGetDynamicListResultListener
	 */
	public void getDynamicDetail(String dynamicid,
			final IGetDynamicListResultListener mIGetDynamicListResultListener) {

		GetDynamicDetailInvokeItem item = new GetDynamicDetailInvokeItem(
				dynamicid);

		HttpRequestHandle.onBaseRequest(item,
				HttpRequestType.SERVER_FAIL_AND_CACHE,
				new HttpRequestHandle.IRequestResultListener() {

					@Override
					public void onResult(Object resultObject, boolean isCache,
							String failReason) {
						if (resultObject != null) {
							GetDynamicDetailInvokeItem.GetDynamicDetailInvokeItemResult result = ((GetDynamicDetailInvokeItem) resultObject)
									.getOutPut();
							if (result != null) {
								if (mIGetDynamicListResultListener != null) {
									mIGetDynamicListResultListener
											.onGetDynamicResult(result.status,
													result.msg, result.dynamics);
								}
							}
						} else {
							if (mIGetDynamicListResultListener != null) {
								mIGetDynamicListResultListener
										.onGetDynamicResult(-1, failReason,
												null);
							}
						}
					}
				});

	}

	public interface IReplyDynamicResultListener {
		void onReplyDynamicReslut(int status, String msg, Reply mReply);
	}

	/**
	 * 评论动态
	 *
	 * @param source_cust_id
	 * @param dynamicid
	 * @param content
	 * @param mIReplyDynamicResultListener
	 */
	public void replyDynamic(int source_cust_id, int dynamicid, String content,
			final IReplyDynamicResultListener mIReplyDynamicResultListener) {
		ReplyDynamicsInvokeItem item = new ReplyDynamicsInvokeItem(
				source_cust_id, dynamicid, content);
		HttpRequestHandle.onBaseRequest(item,
				HttpRequestType.SERVER_FAIL_AND_CACHE,
				new HttpRequestHandle.IRequestResultListener() {

					@Override
					public void onResult(Object resultObject, boolean isCache,
							String failReason) {
						if (resultObject != null) {
							ReplyDynamicsInvokeItem.ReplyDynamicsResult result = ((ReplyDynamicsInvokeItem) resultObject)
									.getOutPut();
							if (result != null) {
								if (mIReplyDynamicResultListener != null) {
									mIReplyDynamicResultListener
											.onReplyDynamicReslut(
													result.status, result.msg,
													result.mReply);
								}
							} else if (mIReplyDynamicResultListener != null) {
								mIReplyDynamicResultListener
										.onReplyDynamicReslut(-1, "", null);
							}

						} else {
							if (mIReplyDynamicResultListener != null) {
								mIReplyDynamicResultListener
										.onReplyDynamicReslut(-1, "", null);
							}
						}
					}
				});

	}

	public interface IDynamicResult {
		void onDynamicResult(int status, String msg);
	}

	/**
	 * 赞动态
	 *
	 * @param dynamicid
	 * @param op
	 * @param mIDynamicResult
	 */
	public void praiseDynamic(int dynamicid, int op,
			final IDynamicResult mIDynamicResult) {
		PraiseDynamicsInvokeItem item = new PraiseDynamicsInvokeItem(dynamicid,
				op);
		HttpRequestHandle.onBaseRequest(item, HttpRequestType.SERVER_ONLY,
				new HttpRequestHandle.IRequestResultListener() {

					@Override
					public void onResult(Object resultObject, boolean isCache,
							String failReason) {
						if (resultObject != null) {
							HttpInvokeResult result = ((PraiseDynamicsInvokeItem) resultObject)
									.getOutput();
							if (result != null) {
								if (mIDynamicResult != null) {
									mIDynamicResult.onDynamicResult(
											result.status, result.msg);
								}
							}

						} else {
							if (mIDynamicResult != null) {
								mIDynamicResult.onDynamicResult(-1, failReason);
							}
						}
					}
				});
	}

	/**
	 * 删除评论
	 *
	 * @param rid
	 * @param mIDynamicResult
	 */
	public void deleteReply(String rid, final IDynamicResult mIDynamicResult) {
		DelReplyInvokeItem item = new DelReplyInvokeItem(Long.parseLong(rid));

		HttpRequestHandle.onBaseRequest(item, HttpRequestType.SERVER_ONLY,
				new HttpRequestHandle.IRequestResultListener() {

					@Override
					public void onResult(Object resultObject, boolean isCache,
							String failReason) {
						if (resultObject != null) {
							HttpInvokeResult result = ((DelReplyInvokeItem) resultObject)
									.getOutput();
							if (result != null) {
								if (mIDynamicResult != null) {
									mIDynamicResult.onDynamicResult(
											result.status, result.msg);
								}
								if (result.status == HttpInvokeResultCode.RESULT_OK) {
									IMApplication
											.showToast(R.string.delete_success);
								}
							}

						} else {
							if (mIDynamicResult != null) {
								mIDynamicResult.onDynamicResult(-1, failReason);
							}
						}
					}
				});

	}

	/**
	 * 删除动态
	 *
	 * @param dynamicid
	 * @param mIDynamicResult
	 */
	public void deleteDynamic(int dynamicid,
			final IDynamicResult mIDynamicResult) {

		DelDynamicsInvokeItem item = new DelDynamicsInvokeItem(dynamicid);
		HttpRequestHandle.onBaseRequest(item, HttpRequestType.SERVER_ONLY,
				new HttpRequestHandle.IRequestResultListener() {

					@Override
					public void onResult(Object resultObject, boolean isCache,
							String failReason) {
						if (resultObject != null) {
							HttpInvokeResult result = ((DelDynamicsInvokeItem) resultObject)
									.getOutput();
							if (result != null) {
								if (mIDynamicResult != null)
									mIDynamicResult.onDynamicResult(
											result.status, result.msg);
							} else {
								if (mIDynamicResult != null)
									mIDynamicResult.onDynamicResult(-1,
											failReason);
							}
						} else {
							if (mIDynamicResult != null) {
								mIDynamicResult.onDynamicResult(-1, failReason);
							}
						}
					}
				});

	}

	/**
	 * 发动态
	 *
	 * @param mStrContent
	 * @param fileMessages
	 * @param mIDynamicResult
	 */
	public static void sendDynamic(String mStrContent,
								   ArrayList<FileMessage> fileMessages,FileMessage videoFileMessage,FileMessage videoConverFileMessage,
								   final IDynamicResult mIDynamicResult) {
		PublishDynamicInvokeItem item = new PublishDynamicInvokeItem(
				mStrContent, fileMessages,videoFileMessage,videoConverFileMessage);
		HttpRequestHandle.onBaseRequest(item, HttpRequestType.SERVER_ONLY,
				new HttpRequestHandle.IRequestResultListener() {

					@Override
					public void onResult(Object resultObject, boolean isCache,
										 String failReason) {
						if (resultObject != null) {
							PublishDynamicInvokeItem.PublishResult result = ((PublishDynamicInvokeItem) resultObject)
									.getOutput();
							if (result != null) {
								if (mIDynamicResult != null) {
									mIDynamicResult.onDynamicResult(
											result.status, result.msg);
								}
							} else {
								if (mIDynamicResult != null) {
									mIDynamicResult.onDynamicResult(-1, "");
								}
							}
						} else {
							if (mIDynamicResult != null) {
								mIDynamicResult.onDynamicResult(-1, failReason);
							}
						}
					}
				});
	}

	public interface IDeleteDynamicReplyHistoryListener {
		void onDeleteDynamicReplyHisResult(int status,
														   String msg);
	}

	/**
	 * 删除历史动态评论数据
	 *
	 * @param reply_id
	 */
	public void deleteDynamicReplyHistory(
			String reply_id,
			final IDeleteDynamicReplyHistoryListener mIDeleteDynamicReplyHistoryListener) {
		DelRelationListInvokeItem item = new DelRelationListInvokeItem(reply_id);
		HttpRequestHandle.onBaseRequest(item, HttpRequestType.SERVER_ONLY,
				new HttpRequestHandle.IRequestResultListener() {

					@Override
					public void onResult(Object resultObject, boolean isCache,
							String failReason) {
						if (resultObject != null) {
							HttpInvokeResult result = ((DelRelationListInvokeItem) resultObject)
									.getOutput();
							if (result != null) {
								mIDeleteDynamicReplyHistoryListener
										.onDeleteDynamicReplyHisResult(
												result.status, result.msg);
							} else {
								mIDeleteDynamicReplyHistoryListener
										.onDeleteDynamicReplyHisResult(-1,
												failReason);
							}
						} else {
							if (mIDeleteDynamicReplyHistoryListener != null) {
								mIDeleteDynamicReplyHistoryListener
										.onDeleteDynamicReplyHisResult(-1,
												failReason);
							}
						}
					}
				});
	}
}
